<template>
  <div class="data-card">
    <div class="data-card__body">
      <div class="data-card__icon" :class="iconClass">
        <el-icon v-if="isElementIcon">
          <component :is="icon" />
        </el-icon>
        <image-preview v-else-if="isImageIcon" :src="icon" class="data-card__icon-img"></image-preview>
      </div>

      <div class="data-card__content">
        <div class="data-card__title">{{ title }}</div>
        <div class="data-card__value-container">
          <span class="data-card__value">{{ value }}</span>
          <span class="data-card__unit">{{ unit }}</span>
        </div>
      </div>
    </div>

    <div class="data-card__footer" v-if="hasComparisonData">
      <div class="data-card__divider"></div>
      <div class="data-card__comparison">
        <div class="data-card__comparison-item" v-if="yearOnYear">
          <span class="data-card__comparison-label" v-if="yearOnYearLabel">{{ yearOnYearLabel }}</span>
          <span class="data-card__comparison-value" :class="getComparisonClass(yearOnYear)">
            {{ yearOnYear }}
          </span>
        </div>
        <div class="data-card__comparison-item" v-if="monthOnMonth">
          <span class="data-card__comparison-label" v-if="monthOnMonthLabel">{{ monthOnMonthLabel }}</span>
          <span class="data-card__comparison-value" :class="getComparisonClass(monthOnMonth)">
            {{ monthOnMonth }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ShopDataInfo',
  props: {
    title: {
      type: String,
      default: '标题'
    },
    value: {
      type: String,
      default: ''
    },
    unit: {
      type: String,
      default: ''
    },
    yearOnYearLabel: {
      type: String,
      default: ''
    },
    yearOnYear: {
      type: String,
      default: ''
    },
    monthOnMonthLabel: {
      type: String,
      default: ''
    },
    monthOnMonth: {
      type: String,
      default: ''
    },
    icon: {
      type: String,
      default: 'DataBoard'
    },
    iconColor: {
      type: String,
      default: 'primary',
      validator: (value) => {
        return ['primary', 'success', 'warning', 'danger', 'info', 'none'].includes(value);
      }
    },
    iconType: {
      type: String,
      default: 'icon', // 'icon' 或 'image'
      validator: (value) => {
        return ['icon', 'image'].includes(value);
      }
    }
  },
  computed: {
    iconClass() {
      return `icon-${this.iconColor}`;
    },
    isImageIcon() {
      return this.iconType === 'image';
    },
    isElementIcon() {
      return this.iconType === 'icon';
    },
    hasComparisonData() {
      return this.yearOnYear || this.monthOnMonth;
    }
  },
  methods: {
    getComparisonClass(value) {
      if (!value) return '';

      // 判断是否为增长（包含+号或正数）
      if (value.includes('+') || parseFloat(value) > 0) {
        return 'comparison-positive';
      }

      // 判断是否为下降（包含-号或负数）
      if (value.includes('-') || parseFloat(value) < 0) {
        return 'comparison-negative';
      }

      return '';
    }
  }
}
</script>

<style scoped>
.data-card {
  background-color: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  position: relative;
  width: 260px;
}

.data-card:hover {
  box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.data-card__body {
  display: flex;
  align-items: flex-start;
}

.data-card__icon {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  font-size: 24px;
  flex-shrink: 0;
}

.data-card__icon-img {
  width: 48px;
  height: 48px;
  object-fit: contain;
}

.data-card__content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.data-card__title {
  font-size: 14px;
  color: #606266;
  font-weight: normal;
  margin: 0 0 8px 0;
  line-height: 1.4;
}

.data-card__value-container {
  display: flex;
  align-items: flex-end;
  line-height: 1;
}

.data-card__value {
  font-size: 20px;
  color: #303133;
  font-weight: bold;
  margin-right: 4px;
}

.data-card__unit {
  font-size: 15px;
  color: #606266;
  margin-bottom: 2px;
}

.data-card__footer {
  margin-top: 8px;
}

.data-card__divider {
  height: 1px;
  background-color: #ebeef5;
  margin: 12px 0;
}

.data-card__comparison {
  display: flex;
  justify-content: space-between;
}

.data-card__comparison-item {
  display: flex;
  align-items: center;
}

.data-card__comparison-label {
  font-size: 12px;
  color: #909399;
  margin-right: 6px;
}

.data-card__comparison-value {
  font-size: 12px;
  font-weight: bold;
}

.comparison-positive {
  color: #67c23a;
}

.comparison-negative {
  color: #f56c6c;
}

/* 图标颜色样式 */
.icon-primary {
  background-color: #ecf5ff;
  color: #409eff;
}

.icon-success {
  background-color: #f0f9eb;
  color: #67c23a;
}

.icon-warning {
  background-color: #fdf6ec;
  color: #e6a23c;
}

.icon-danger {
  background-color: #fef0f0;
  color: #f56c6c;
}

.icon-info {
  background-color: #f4f4f5;
  color: #909399;
}

.icon-none {
  background-color: transparent;
}
</style>
